import { mapGetters } from "vuex";
import { isProd, clickIdKeys } from "@/config";
import qs from "qs";
export default {
  data() {
    return {
      adMpLink: '',
      adMpLoading: false
    }
  },
  computed: {
    ...mapGetters('open', ['behaviorList']),
  },
  methods: {
    // 数据上报
    async adReport(action_type = 'PAGE_VIEW') {
      const uniEnv = this.$uni.env
      if (uniEnv.miniprogram) return

      await this.$api
      [uniEnv.miniprogram ? 'adReport' : 'autoAction']({
        action_type
      }).catch((err) => {
        console.log('adReport error', err)
      })
      console.log('adReport ', action_type)
    },
    // 获取上级页面参数
    getReferrer() {
      let ref = "";
      if (document.referrer.length > 0) {
        ref = document.referrer;
      }
      try {
        if (ref.length == 0 && opener?.location?.href?.length > 0) {
          ref = opener.location.href;
        }
      } catch (e) {
        console.log("referrer error", e);
      }
      if (!ref) return;
      if (isProd && ref.includes(location.origin)) return;
      let refArr = ref.split("?");
      refArr.shift();
      let query = qs.parse(refArr.join("?"));
      console.log("referrer query", query);
      this.changeClickId(query);
    },
    // 缓存click_id
    changeClickId(query) {
      for (let key in query) {
        if (clickIdKeys.includes(key)) {
          console.log(query[key])
          this.$cache.set("click_id", query[key]);
          break;
        }
      }
    },
    async skipAdMp(args = {}) {
      const { id, type, id_stage, id_supplier, debug, source_id, mini_id } =
        this.$route.query;
      let mpToken = this.$cache.get("mpToken") || "";
      console.log('skipAdMp start', { mpToken });

      if (mpToken) return;
      console.log('skipAdMp mpToken', mpToken);

      if (source_id == id) return;
      console.log('skipAdMp source_id', source_id == id);

      if (this.adMpLoading) return;
      console.log('skipAdMp adMpLoading', this.adMpLoading);

      if (!this.adMpLink) {
        let click_id = this.$cache.get("click_id");
        const { biz_id, skip_mp_id, mp_query } = args
        let query = {
          id,
          type,
          id_stage,
          id_supplier,
          click_id,
          debug,
          biz_id,
          mini_id: skip_mp_id || mini_id
        };
        if (mp_query) {
          query = {
            ...query,
            ...qs.parse(mp_query),
          };
        }
        query.source_id = id;
        this.adMpLoading = true;
        query.host = window.location.origin

        const res = await this.$api
          .getMiniScheme({
            query: qs.stringify(query),
            mini_id: query.mini_id
          })
          .finally(() => {
            this.adMpLoading = false;
          });
        this.adMpLink = res?.url_scheme || "";
      }

      if (!this.adMpLink) return
      let link = this.adMpLink + '&time=' + Date.now()
      console.log('adMpLink link', link);
      try {
        setTimeout(() => {
          window.location.href = link;
        }, 0);
      } catch (error) {
        console.log('adMpLink error', error);
      }
    },
    getStyle(el, styleProp) {
      let camelize = function (str) {
        // eslint-disable-next-line no-useless-escape
        return str.replace(/\-(\w)/g, (str, letter) => {
          return letter.toUpperCase();
        });
      };
      if (el.currentStyle) {
        return el.currentStyle[camelize(styleProp)];
      } else if (
        document.defaultView &&
        document.defaultView.getComputedStyle
      ) {
        return document.defaultView
          .getComputedStyle(el, null)
          .getPropertyValue(styleProp);
      } else {
        return el.style[camelize(styleProp)];
      }
    },
  }
}
